﻿using System;

namespace OpenLS.Spreadsheet.Expressions
{
    sealed class FullColumnRangeReference: FullRangeReference<Column, Row>
    {
        internal FullColumnRangeReference(DimensionReference<Column, Row> first, DimensionReference<Column, Row> last) : base(first, last)
        {
        }


        protected override void OnRangeDeleting(RangeChangeEventArgs e)
        {
            if (e.Hint == CellMoveOrientationHint.Horizontal)
            {
                if (e.Address.IsFullColumn)
                {
                    if (e.Address.Contains(new CellAddress(0, First.Anchor.Index)))
                    {
                        int offset = e.Address.BottomRight.Column - First.Anchor.Index + 1;
                        offset = Math.Max(offset, - First.Anchor.Index);
                        First = First.OffsetDim(new CellRelativeAddress(0, offset));
                    }
                    if (e.Address.Contains(new CellAddress(0, Last.Anchor.Index)))
                    {
                        int offset = e.Address.TopLeft.Column - Last.Anchor.Index - 1;
                        offset = Math.Max(offset, -Last.Anchor.Index);
                        Last = Last.OffsetDim(new CellRelativeAddress(0, offset));
                    }
                }
            }
        }

        public override int MinRowIndex
        {
            get { return 0; }
        }

        public override int MaxRowIndex
        {
            get { return Worksheet.MaxRowIndex; }
        }

        public override int MinColumnIndex
        {
            get { return this.First.Anchor.Index; }
        }

        public override int MaxColumnIndex
        {
            get { return Last.Anchor.Index; }
        }

        public override RangeReference Offset(CellRelativeAddress address)
        {
            DimensionReference<Column, Row> newFirst = First.OffsetDim(address);
            DimensionReference<Column, Row> newLast = Last.OffsetDim(address);
            return new FullColumnRangeReference(newFirst, newLast);
        }

        internal override RangeReference Transposed(Cell target)
        {
            return new FullRowRangeReference(this.First.Transposed<Row>(target), Last.Transposed<Row>(target));
        }

        internal override RangeReference Projected(Worksheet targetWorksheet)
        {
            return new FullColumnRangeReference(((ColumnReference) First).Projected(targetWorksheet),
                                                ((ColumnReference)this.Last).Projected(targetWorksheet));
        }
    }
}